package com.homegoods.classify;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.kedll.library.util.StringUtil;

/**
 * 切换Fragment工具类
 */
public class CreateFragment {

    FragmentTransaction ft;
    FragmentManager fm;
    int resId;
    private Fragment mContent;
    private String currentFunCode;
    private Context mContext;

    /**
     * 构造函数
     *
     * @param fm
     * @param resId 要显示fragment的布局ID
     */
    public CreateFragment(Context ctx, FragmentManager fm, int resId) {
        this.mContext = ctx;
        this.fm = fm;
        this.resId = resId;
    }

    public Fragment getFragment(String fragmentId, Bundle bundle) {
        return Fragment.instantiate(mContext, fragmentId, bundle);
    }

    /**
     * 根据功能码切换fragment
     *
     * @link replaceFragment(String funCode, boolean replace)
     */
    public void replaceFragment(String funCode) {
        replaceFragment(funCode, true);
    }

    /**
     * 根据功能码切换fragment
     *
     * @param funCode
     * @param replace 表示替换fragment是用add还是replace; true is replace, false is add
     */
    public void replaceFragment(String funCode, boolean replace) {
        if (StringUtil.isNotEqual(currentFunCode, funCode)) {
            currentFunCode = funCode;
            if (replace) {
                replaceFragment2();
            } else {
                replaceFragment();
            }
        }
    }

    private void replaceFragment2() {
        Fragment fragment = fm.findFragmentByTag(currentFunCode);
        if (fragment == null) {
            fragment = getFragment(currentFunCode);
        }
        if (fragment != null) {
            replaceFragment(fragment);
        }
    }

    private void replaceFragment() {
        Fragment fragment = fm.findFragmentByTag(currentFunCode);
        if (fragment == null) {
            fragment = getFragment(currentFunCode);
        }

        if (fragment != null) {
            replaceFragment(mContent, fragment);
        }
    }

    public Fragment getFragment(String fragmentId) {
        return Fragment.instantiate(mContext, fragmentId);
    }

    private void replaceFragment(Fragment dst, Fragment src) {
        try {
            ft = fm.beginTransaction();
            if (!src.isAdded()) {
                ft.add(resId, src, currentFunCode);
            }else{
                ft.show(src);
            }
            if (dst != null) {
                ft.hide(dst);
                dst.setUserVisibleHint(false);
            }
            mContent = src;
            src.setUserVisibleHint(true);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void showFragment(Fragment src) {
        replaceFragment(mContent, src);

    }

    public void replaceFragment(Fragment fragment) {
        ft = fm.beginTransaction();
        ft.replace(resId, fragment);
        mContent = fragment;
        fragment.setUserVisibleHint(true);
        ft.commit();
    }

    /**
     * 带有动画的Fragment切换
     *
     * @param fragment
     * @param enter
     * @param exit
     */
    public void replaceFragmentWithAminotion(Fragment fragment, int enter, int exit) {
        ft = fm.beginTransaction();
        ft.setCustomAnimations(enter, exit);
        ft.replace(resId, fragment);
        mContent = fragment;
        ft.commit();
    }

    /**
     * 获取当前的fragment
     *
     * @return
     */
    public Fragment getCurrentFragment() {
        return mContent;
    }

    public void onDestory() {
        mContent = null;
        fm = null;
        ft = null;

    }
}
